class Survey < ActiveRecord::Base
	include AliasingColumns

  	set_table_name "SURVEYS"
	AliasingColumns.add_aliases columns


	belongs_to :user,
			   :class_name => "User",
			   :foreign_key => "user_id"
	belongs_to :distributiontype,
			   :class_name => "DistributionType",
			   :foreign_key => "distribution_type_id"
	has_many :questions,
			 :class_name => "Question",
			 :foreign_key => "survey_id",	   
			 :dependent => :destroy
			 
	has_many :filledsurveys,
			 :class_name => "FilledSurvey",
			 :foreign_key => "survey_id",	   
			 :dependent => :destroy
	
	
	has_many :rights,
			 :class_name => "UsersSurveysPermission",
			 :foreign_key => "survey_id",	   
			 :dependent => :destroy
			 	 
	accepts_nested_attributes_for :questions, :allow_destroy => true
	accepts_nested_attributes_for :filledsurveys, :allow_destroy => true
	accepts_nested_attributes_for :rights, :allow_destroy => true
	
	
	validates_presence_of :title, :description
#	validate :dates_are_correct
	validate :start_only_ready
	

	def dates_are_correct
		if start_date>end_date
			errors.add_to_base("Data zakończenia nie może być wcześniejsza niż data rozpoczęcia!")
		end
		if start_date<Time.now.to_s(:db)
			errors.add_to_base("Data rozpoczęcia nie moze wcześniejsza niż aktualna data!")		
		end
		
	end
	
	def start_only_ready
			
		  		
		if start_date.to_s<Time.now.to_s(:db) && ready=='n'	
		    		  		STDERR.puts "\n\n\n\n,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
		  		
		
	    	
			errors.add_to_base("Nie można rozpocząć ankiety, która nie jest gotowa\n
								- zmien date rozpoczęcia lub oznacz ankietę jako gotową!")
		end
	end
	
	def closed_questions
		cq_type_id=QuestionType.find_by_name('closed question').id
		questions.find_all_by_type_id(cq_type_id)
	end
	
	
	def number_of_ready
		self.filledsurveys.find(:all,:conditions=>"READY='y'").length
	end
	
	def number_of_notready
		self.filledsurveys.find(:all,:conditions=>"READY='n'").length		
	end
	
	
	def number_of_entiltement_surveyees
		ent_to_fill.length	
	end
	
	def ent_to_fill
		self.rights.find(:all,:joins=>"JOIN SURVEY_PERMISSION_TYPES
									ON USERS_SURVEYS_PERMISSIONS.PERMISSION_TYPE_ID=SURVEY_PERMISSION_TYPES.ID",
							  :conditions=>"SURVEY_PERMISSION_TYPES.NAME='fill'")
	end
	def temp_ent_to_fill
		 temp_id=SystemRole.find_by_name("TempSurveyee").id
		result=self.rights.find(:all,:joins=>"JOIN SURVEY_PERMISSION_TYPES
									ON USERS_SURVEYS_PERMISSIONS.PERMISSION_TYPE_ID=SURVEY_PERMISSION_TYPES.ID
									JOIN USERS
									ON USERS_SURVEYS_PERMISSIONS.USER_ID=USERS.ID",
									
							  :conditions=>"SURVEY_PERMISSION_TYPES.NAME='fill' AND  USERS.ROLE_ID=#{temp_id}")
	end
	
	def ent_to_edit
		self.rights.find(:all,:joins=>"JOIN SURVEY_PERMISSION_TYPES
									ON USERS_SURVEYS_PERMISSIONS.PERMISSION_TYPE_ID=SURVEY_PERMISSION_TYPES.ID",
							  :conditions=>"SURVEY_PERMISSION_TYPES.NAME='edition'")
	end
	
	def ent_to_view_stats
		self.rights.find(:all,:joins=>"JOIN SURVEY_PERMISSION_TYPES
									ON USERS_SURVEYS_PERMISSIONS.PERMISSION_TYPE_ID=SURVEY_PERMISSION_TYPES.ID",
							  :conditions=>"SURVEY_PERMISSION_TYPES.NAME='stats'")	
	end
	
	def turnout
		((number_of_ready.to_f/number_of_entiltement_surveyees.to_f) *100.0).to_s<<"%"		
	end
	
	if  AliasingColumns.get_adapter_name == "oracle"
	#	self.primary_key ="ID"
		set_sequence_name table_name()<<"_SEQ" 
	#	before_validation :set_id
		def set_id
			 self.ID =connection.next_sequence_value(self.class.sequence_name)	
		end 
	end
	
end
